package com.yichuang.security.javajwt.main.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.yichuang.security.javajwt.main.common.JwtTokenProvider;
import com.yichuang.security.javajwt.main.service.OAuth2Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p> 鉴权调试接口 </p>
 *
 * @author tuonioooo
 * @site https://miliqkdoc.motopa.cn/
 * @date 2020/6/18 11:20
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class OAuth2Controller {

    private final OAuth2Service oAuth2Service;

    private final AuthenticationManager authenticationManager;

    private final JwtTokenProvider jwtTokenProvider;

    @PostMapping("/login")
    public String authenticate(@RequestParam String username, @RequestParam String password) {
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(username, password)
        );
        return jwtTokenProvider.generateToken(authentication);
    }

    @GetMapping("/login/{id}")
    public String login(@PathVariable String id) {
        return "login id : " + id;
    }

    @GetMapping("/product/{id}")
    public String getProduct(@PathVariable String id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info(JSON.toJSONString(authentication));
        return "product id : " + id;
    }

    @GetMapping("/order/{id}")
    public String getOrder(@PathVariable String id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info(JSON.toJSONString(authentication));
        return "order id : " + id;
    }

    @GetMapping("/oauth2/getToken")
    public String login() {
        String token = oAuth2Service.getToken("admin", "{noop}123456");
        JSONObject jsonObject = JSONObject.parseObject(token);
        if(StringUtils.isNotBlank(token) && jsonObject.containsKey("access_token")){
            log.info("采用Spring Security 、Spring Security OAuth2、java-jwt集成方式创建的令牌, token={}", token);
            DecodedJWT decodedJWT = jwtTokenProvider.decodeToken(jsonObject.getString("access_token"));
            log.info("Subject={}", decodedJWT.getSubject());
            log.info("ExpiresAt={}", decodedJWT.getExpiresAt());
            log.info("IssuedAt={}", decodedJWT.getIssuedAt());
            log.info("Payload={}", decodedJWT.getPayload());
            log.info("Algorithm={}", decodedJWT.getAlgorithm());
        }
        return token;
    }

    @GetMapping("/oauth2/refreshToken")
    public String refreshToken(String token) {
        return oAuth2Service.refreshToken("admin", "{noop}123456", token);
    }

}
